﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ExportImportToFromFile
{
    class StudentsList
    {
        public List<Student> students = new List<Student>();

        public static StudentsList ImportFromFile(string path)
        {
            StudentsList list = new StudentsList();
            string file = File.ReadAllText(path);
            string[] fileArr = file.Split(new string[] { "---" + System.Environment.NewLine }, StringSplitOptions.None);
            foreach (string studentStr in fileArr)
            {
                string [] strArr = studentStr.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);
                Student newSt = new Student();
                newSt.id = Int32.Parse(strArr[0]);
                newSt.firstName = strArr[1];
                newSt.lastName = strArr[2];
                list.students.Add(newSt);
            }
            return list;
        }

        public void ExportToFile(string path)
        {
            int size = students.Count();
            string [] fileArr = new string [size];

            for (int i = 0; i < size; i++)
            {
                Student student = students[i];
                fileArr[i] = string.Format("{0}{1}{2}{3}{4}{5}", student.id, System.Environment.NewLine, student.firstName, System.Environment.NewLine, student.lastName, System.Environment.NewLine);
            }

            string file = string.Join("---" + System.Environment.NewLine, fileArr);
            File.WriteAllText(path, file);
        }

        public void PrintToConsole()
        {
            foreach (Student student in students)
            {
                student.PrintStudent();
            }
        }
    }
}
